package org.kong.controller;

import org.kong.pojo.entity.Classroom;
import org.kong.pojo.entity.Stu;
import org.kong.pojo.entity.Subject;
import org.kong.pojo.vo.FindAllStuInfoVo;
import org.kong.pojo.vo.StuSubVO;
import org.kong.pojo.vo.StuVO;
import org.kong.pojo.vo.SubjectVO;
import org.kong.service.StuService;
import org.kong.utils.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 加油
 *
 * @author konglingfeng
 * @date 2025/8/15
 * @desc
 */
@RestController
public class StuController {
    @Autowired
    private StuService stuService;

    /**
     * 查找一位学生
     * @param id
     * @return
     */
    @GetMapping("/stu/{id}")
    public Result<Stu> getStuById(@PathVariable int id) {

        Stu stu = stuService.searchStuById(id);

        if (stu == null) {
            return Result.error("查询失败");
        }

        return Result.success("查询成功",stu);
    }

    /**
     * 查找全部数据
     * @return
     */
    @GetMapping("/stu")
    public Result<List<Stu>> getAllStu() {
        List<Stu> stus = stuService.findAllStu();
        if (stus == null) {
            return Result.error("查询失败");
        }

        return Result.success("查询成功",stus);
    }

    /**
     * 条件查询
     * @param stu
     * @return
     */
    @PostMapping("/emp/condition")
    public Result<List<Stu>> getStuByCondition(@RequestBody Stu stu) {
        List<Stu> stus = stuService.getStuByCondition(stu);

        if (stus == null) {
            return Result.error("查询失败");
        }

        return Result.success("查询成功",stus);
    }

    /**
     * 添加功能
     * @param stu
     * @return
     */
    @PostMapping("/stu/add")
    public Result<String> addStu(@RequestBody Stu stu) {

        boolean isSuccess = stuService.addStu(stu);

        if (!isSuccess) {
            return Result.error("添加失败");
        }

        return Result.success("添加成功");
    }

    /**
     * 更新功能
     * @param id
     * @param stu
     * @return
     */
    @PutMapping("/stu/{id}")
    public Result<String> updateStu(@PathVariable int id, @RequestBody Stu stu) {

        stu.setId(id);

        boolean isSuccess = stuService.updateStu(stu);

        if (!isSuccess) {
            return Result.error("更新失败");
        }

        return Result.success("更新成功");
    }

    /**
     * 删除功能
     * 通过id删除stu
     * @param id
     * @return
     */
    @DeleteMapping("/stu/{id}")
    public Result<String> deleteStuById(@PathVariable int id) {

        boolean isSuccess = stuService.deleteStu(id);

        if (!isSuccess) {
            return Result.error("删除失败");
        }

        return Result.success("删除成功");
    }

    @DeleteMapping("/stu/deletes/{ids}")
    public Result<String> deleteStuByIds(@PathVariable List<Integer> ids) {
        System.out.println("ids = " + ids);

        boolean isSuccess = stuService.deleteStuByIds(ids);

        if (!isSuccess) {
            return Result.error("删除失败");
        }

        return Result.success("删除成功");
    }

    /**
     * 完成stu和classroom的一对一查询
     * @return
     */
    @GetMapping("/stu/classroom")
    public Result<List<StuVO>> getStuClassroom() {

        List<StuVO> stus = stuService.getStuClassroom();
        if (stus == null) {
            return Result.error("查询失败");
        }

        return Result.success("查询成功",stus);
    }

    /**
     * 完成一对多查询
     * @return
     */
    @GetMapping("/stu/sub")
    public Result<List<StuSubVO>> getStuSub() {

        List<StuSubVO> stus = stuService.getStuSub();
        if (stus == null) {
            return Result.error("查询失败");
        }

        return Result.success("查询成功",stus);
    }

    /**
     * 完成一对多对一查询
     * @return
     */
    @GetMapping("/stu/find")
    public Result<List<FindAllStuInfoVo>> findAllInfo() {
        List<FindAllStuInfoVo> findAllStuInfoVos = stuService.findAllInfo();
        if (findAllStuInfoVos == null) {
            return Result.error("查询失败");
        }

        return Result.success("查询成功",findAllStuInfoVos);
    }

    /**
     * 完成对 一对多对一查询 的拆分
     * @return
     */
    @PostMapping("/stu/find")
    public Result<List<FindAllStuInfoVo>> findAllInfo1() {
        List<FindAllStuInfoVo> findAllStuInfoVos = new ArrayList<>();

        List<Stu> stuList = stuService.findAllStu();

        for (Stu stu : stuList) {
            /**
             * 创建一个FindAllStuInfoVo对象接收数据
             */
            FindAllStuInfoVo findAllStuInfoVo = new FindAllStuInfoVo();
            System.out.println("findAllStuInfoVo = " + findAllStuInfoVo);

            /**
             * 将stu复制给findAllStuInfoVo
             */
            BeanUtils.copyProperties(stu,findAllStuInfoVo);

            /**
             * 通过cid查找对应的教室信息
             */
            Classroom classroom = stuService.getClassroomByCid(stu.getCid());
            System.out.println("classroom = " + classroom);

            /**
             * 查找该学生所学课程及课程老师
             */
            List<SubjectVO> subjectVOs = stuService.searchSubjectVOBySid(stu.getId());
            System.out.println("subjectVOs = " + subjectVOs);

            findAllStuInfoVo.setClassroom(classroom);

            findAllStuInfoVo.setSubjectVO(subjectVOs);

            findAllStuInfoVos.add(findAllStuInfoVo);

        }

        if (findAllStuInfoVos == null) {
            return Result.error("查询失败");
        }

        return Result.success("查询成功",findAllStuInfoVos);
    }
}
